#include "dialog.h"
#include "ui_dialog.h"

#include <QFileDialog>
#include <QImage>
#include <QDebug>
#include <QPalette>
#include <QMessageBox>
#include <QColorDialog>

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    QColor color(152,169,238,255);
    setFrontBtnColor(color);
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::on_pushButton_clicked()
{
    QStringList files=QFileDialog::getOpenFileNames(this,"png file","",tr("Images (*.png)"));
    int cnt=files.count();
    if(cnt>0){
        for (int i = 0; i < cnt; ++i) {
            QImage *thepng=new QImage(files[i]);
            if(thepng!=nullptr && !thepng->isNull()){
                thepng->save(files[i]);
                delete thepng;
            }
        }
    }
}

void Dialog::on_btnCvtGray_clicked()
{
    QStringList files=QFileDialog::getOpenFileNames(this,"png file","",tr("Images (*.png)"));
    int cnt=files.count();
    if(cnt>0){
        bool wantSave=false;
        QMessageBox msgbox;
        msgbox.setWindowFlag(Qt::WindowStaysOnTopHint);
        msgbox.setIcon(QMessageBox::Question);
        msgbox.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
        msgbox.button(QMessageBox::Yes)->setText(tr("Yes"));
        msgbox.button(QMessageBox::No)->setText(tr("No"));
        msgbox.setText(tr("Do you want to overwrite all image?"));
        int btn=msgbox.exec();
        if(btn==QMessageBox::Yes){
            wantSave=true;
        }else{
            wantSave=false;
        }

        if(!wantSave) return;

        for (int i = 0; i < cnt; ++i) {
            QImage *thepng=new QImage(files[i]);
            if(thepng!=nullptr && !thepng->isNull()){
                thepng->save(files[i]);
                QImage tempImg=thepng->convertToFormat(QImage::Format_RGBA8888);
                if(!tempImg.isNull()){
                    int width=tempImg.width();
                    int height=tempImg.height();
                    unsigned char *p=tempImg.bits();
//                    float alpha=0;
                    QColor color=ui->btnFrontColor->palette().color(QPalette::Button);
                    for (int v = 0; v < height; ++v) {
                        for (int u = 0; u < width; ++u) {
                            if(p[v*width*4+u*4+3]!=0){
//                                alpha=p[v*width*4+u*4+3];
//                                alpha/=255;
                                p[v*width*4+u*4+0]=static_cast<unsigned char>(color.red());
                                p[v*width*4+u*4+1]=static_cast<unsigned char>(color.green());
                                p[v*width*4+u*4+2]=static_cast<unsigned char>(color.blue());
                            }
                        }
                    }
                    tempImg.save(files[i]);
                }
                delete thepng;
            }
        }
    }
}

void Dialog::on_btnFrontColor_clicked()
{
    QColor color=ui->btnFrontColor->palette().color(QPalette::Button);
    QColorDialog cdialog(color);
    cdialog.setWindowFlag(Qt::WindowStaysOnTopHint);
    cdialog.setOption(QColorDialog::ShowAlphaChannel);
    cdialog.setWindowTitle(tr("Select color"));
    cdialog.exec();
    color=cdialog.selectedColor();
    if(color.isValid()){
        setFrontBtnColor(color);
    }

}

void Dialog::setFrontBtnColor(QColor c)
{
    QPalette palette=ui->btnFrontColor->palette();
    palette.setBrush(QPalette::Window,c);
    palette.setColor(QPalette::Button,c);
    palette.setBrush(QPalette::Button,c);

    ui->btnFrontColor->setAutoFillBackground(true);
    ui->btnFrontColor->setPalette(palette);

}
